/**
 * 
 */
package cn.dafa.login.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.dafa.login.service.LoginService;
import cn.dafa.user.model.User;

/**
 * @author 韩大发
 * @datetime 2012-8-29 上午10:54:45
 * @email:smile-gorget-song@foxmail.com
 * @summary dafa
 */
@Controller
@RequestMapping(value = "login")
public class LoginContoller {
	private Logger logger = Logger.getLogger(LoginContoller.class);
	@Resource
	private LoginService mLoginService;

	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(@RequestParam("loginname") String loginname,
			@RequestParam("password") String password, HttpSession session,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		logger.info("login");
		if (StringUtils.isBlank(loginname)) {
			response.getWriter().write("{failure:用户名不能为空！}");
		}
		if (StringUtils.isBlank(password)) {
			response.getWriter().write("{failure:密码不能为空！}");
		}
		User user = new User();
		user.setName(loginname);
		user.setPassword(password);
		int result = mLoginService.checkUserExits(user);
		if (result > 0) {
			response.getWriter().write("{success:true}");
			session.putValue("loginname", loginname);
		} else {
			response.getWriter().write("{success:false,errors:{password:'密码错误！'}}");
		}
		return null;
	}

	/**
	 * @param mLoginService
	 *            the mLoginService to set
	 */
	public void setmLoginService(LoginService mLoginService) {
		this.mLoginService = mLoginService;
	}

}
